package com.yitao.cms.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, String> stringRedisTemplate;
    @Autowired
    private RedisTemplate<Object, Object> objectRedisTemplate;
    /*
     * 指定key获取String值
     */
    public String getStr(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
    /**
     * 设置字符串缓存
     * @param key
     * @param value
     */
    public void setStr(String key,String value) {
        stringRedisTemplate.opsForValue().set(key,value);
    }

    /**
     * 获取Object缓存
     * @param string
     * @return
     */
    public  Object getObject(String string){
        return objectRedisTemplate.opsForValue().get(string);
    }

    /**
     * 设置Object缓存
     * @param string
     * @param object
     */
    public void  setObejct(String string,Object object){
        objectRedisTemplate.opsForValue().set(string,object);
    }

    /**
     * 删除缓存
     * @param string
     */
    public  void delObject(String string){
        objectRedisTemplate.delete(string);
    }

    public  boolean idNull(String key){
        boolean hasKey = objectRedisTemplate.hasKey(key);
        return hasKey;
    }

    /**
     * hash数据新增
     * @param key
     * @param hk
     * @param hv
     */
    public void setObjectHash(String key,String hk,Object hv) {
        objectRedisTemplate.opsForHash().put(key, hk, hv);
    }

    /**
     * hash数据删除
     * @param key
     * @param hk
     */
    public void delObjectHash(String key,String hk) {
        objectRedisTemplate.opsForHash().delete(key, hk);
    }

    /**
     * 判断hash数据是否存在
     * @param key
     * @param hk
     * @return
     */
    public  boolean judgeObjectHashIsNotNull(String key,String hk){
        return objectRedisTemplate.opsForHash().hasKey(key,hk);
    }

    /**
     * 获取hash数据
     * @param key
     * @param hk
     * @return
     */
    public  Object getHashObject(String key,String hk){
        return objectRedisTemplate.opsForHash().get(key,hk);
    }

    /**
     * 获取hash数据list
     * @param key
     * @return
     */
    public Object getHashObject(String key){
       return objectRedisTemplate.opsForHash().entries(key);
    }

    /**
     * 获取hash数据Map
     * @param key
     * @return
     */
    public Map<Object, Object> getHashObjectMap(String key){
        Map<Object, Object> map = new HashMap<>();
        Cursor<Map.Entry<Object, Object>> curosr = objectRedisTemplate.opsForHash().scan(key, ScanOptions.NONE);
        while(curosr.hasNext()){
            Map.Entry<Object, Object> entry = curosr.next();
            map.put(entry.getKey(),entry.getValue());
        }
        return map;
    }
    /**
     * 获取hash数据list
     * @param key
     * @return
     */
    public List<Object> getHashObjectList(String key){
        List<Object> list = new ArrayList<>();
        Cursor<Map.Entry<Object, Object>> curosr = objectRedisTemplate.opsForHash().scan(key, ScanOptions.NONE);
        while(curosr.hasNext()){
            Map.Entry<Object, Object> entry = curosr.next();
            list.add(entry.getValue());
        }
        return list;
    }

    /**
     * hash数据size
     * @param key
     */
    public Long getHashSize(String key) {
        return objectRedisTemplate.opsForHash().size(key);
    }



    /*
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired("stringRedisTemplate")
    ValueOperations<String, String> valueStr;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Resource(name="redisTemplate")
    ValueOperations<Object, Object> valueObject;

    *//*
     * 指定key获取String值
     *//*
    public String getStr(String key) {
        return valueStr.get(key);
    }

    *//**
     * 设置缓存
     *//*
    public void setStr(String key,String value) {
        valueStr.set(key, value);
    }
    *//**
     * 删除指定key
     *//*
    public void delStr(String key) {
        stringRedisTemplate.delete(key);
    }
    *//**
     * 根据指定o获取Object
     *//*
    public Object getObj(Object o) {
        return valueObject.get(o);
    }
    *//**
     * 设置obj缓存
     *//*
    public void setObj(Object obj1,Object obj2) {
        valueObject.set(obj1, obj2);
    }
    *//**
     * 删除Object
     *//*
    public void delObj(Object obj) {
        redisTemplate.delete(obj);
    }
    *//*public List<Object> getListOject(String key){
        return
    }*//*

    *//**
     * 存入list
     * @param key
     * @param listObejct
     *//*
    public void setObjectList(String key,List<Object> listObejct) {
        valueObject.set(key, listObejct);
    }
    public List<Object> getObejctList(String key){
        Object obList=valueObject.get(key);

        return (List<Object>) obList;
    }*/


}
